package com.formina.engine;



import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.RectF;
import android.util.FloatMath;


public class Model {

	   
	   protected ModelInfo modelInfo;
	   
	   protected float[] location= new float[]{0,0,0};
	   protected float[] scale= new float[]{1f,1f,1f};
	   protected float[] rotation = new float[]{0,0,0};
	   protected float[] velocity = new float[]{0,0,0};
	   
	   protected float[] color = new float[]{1f,1f,1f,1f};
	   
	   protected float speed = 1f;
	   protected float direction = 0f;
	   protected boolean isDead = false;
	   
	   protected long addedTime = 0;
	   protected long lastTime = 0;

	   
       public Model(float[] location, float[] scale,ModelInfo info){
    	  this.modelInfo = info;
    	  this.setLocation(location[0], location[1], location[2]);
    	  this.setScale(scale);
    	  
       }
	   
	   public Model() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f}, new ModelInfo());
	   }
	   
	   public void update(long time){
		   
	   }

	   public void draw(GL10 gl) {
	    	
	   }
	   
	   
	     
	  
	   
	   public float Height(){
		   return modelInfo.Height * Scale()[1];
	   }
	   
	   public float Width(){
		   return modelInfo.Width * Scale()[0];

	   }
	   
	   public float Depth(){
		   return modelInfo.Depth * Scale()[2];

	   }
	   
	   public RectF GetRectangle(){
		   //l,t,r,b
		   return new RectF(
				   this.location[0]-Width()*.5f
				   ,this.location[1]-Height()*.5f
				   ,this.location[0]+Width()*.5f
				   ,this.location[1]+Height()*.5f
				   );
	   }
	   
	   public RectF GetTQRectangle(){
		   //l,t,r,b
		   return new RectF(
				   this.location[0]-Width()*.333f
				   ,this.location[1]-Height()*.333f
				   ,this.location[0]+Width()*.333f
				   ,this.location[1]+Height()*.333f
				   );
	   }
	   
	   public boolean CheckTQCollision(Model s){
		   
		   
		   if(this.GetTQRectangle().intersect(s.GetTQRectangle())){
			   return true;
		   }else{
			   return false;
		   }
		   
	   }
	   
	   public boolean CheckCollision(Model s){
		   
		   
		   if(this.GetRectangle().intersect(s.GetRectangle())){
			   return true;
		   }else{
			   return false;
		   }
		   
	   }
	   public boolean CheckTouch(float x, float y){
		   
		   RectF touchRectangle = new RectF(x,y,x,y);
		   if(this.GetRectangle().intersect(touchRectangle)){
			   return true;
		   }else{
			   return false;
		   }
		   
	   }
	   
	   public float[] Rotation(){
		   return this.rotation;
	   }
	   public void setRotation(float[] rotation){
		   this.rotation = rotation.clone();
	   }
	   
	   public void setDirection(float degrees){
		   this.direction = degrees;
		   this.velocity = new float[]{FloatMath.cos(Model.ToRadians(direction)),
		   FloatMath.sin(Model.ToRadians(direction)),
		   0f};
	   }
	   public void setLocation(float x, float y, float z){
		   this.location = new float[]{x,y,z};
	   }
	   public void setVelocity(float[] velocity){
		   this.velocity = velocity;
	   }
	   public float[] getVelocity(){
		   return this.velocity;
	   }
	   
	   public float getX(){
		   return this.location[0];
	   }
	   public float getY(){
		   return this.location[1];
	   }
	   public float getZ(){
		   return this.location[2];
	   }
	   
	   public void setX(float x){
		   this.location[0]= x;
	   }
	   public void setY(float y){
		   this.location[1] = y;
	   }
	   public void setZ(float z){
		   this.location[2] = z;
	   }
	   
	   public float getDirection(){
		   return direction;
	   }
	   
	   public float getSpeed(){
		   return speed;
	   }
	   
	   public boolean IsDead(){
			return isDead;
		}
	   
	   public float[] getColor(){
		   return this.color;
	   }
	   public void setColor(float r, float g, float b, float a){
		   this.color = new float[]{r,g,b,a};
	   }
	   
	   public void setRandomColor(){
		   Random rnd = new Random();
		   int r,g,b,a;
		   a = 255/255; //fully visible
		   r = rnd.nextInt(256);
		   g = rnd.nextInt(256);
		   b = rnd.nextInt(256);
		   this.setColorRGB(r,g,b,a);
	   }
	   
	   public void setAlpha(float a){
		   this.color[3] = a;
	   }
	   
	   public void setAlphaRGB(float a){
		   this.color[3] = a/255f;
	   }
	   
	   public void setColorRGB(int r, int g, int b, int a){
		   this.color = new float[]{r/255f,g/255f,b/255f,a/255f};
	   }
	   
	   public float[] Color(){
		   return this.color;
	   }
	   
	   public void setColor(float[] color){
		   this.color = color.clone();
	   }
	  
	   public static float ToRadians(float degrees){
		   float rad = degrees / 180f * 3.14f;
		   return rad;
	   }
	   
	   public float[] Location(){
		   return location;
	   }
	   public float[] Scale(){
		   return scale;
	   }
	   
	   public void setScale(float[] scale){
		   this.scale = scale.clone();
	   }
	   
	   
	   
	}

